package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpFragment;
import com.sanjiahantang.youjiarole.module.common.adapters.ShopAdapter;
import com.sanjiahantang.youjiarole.module.common.presenters.ShopPresenter;
import com.sanjiahantang.youjiarole.module.common.views.ShopView;
import com.zbar.lib.CaptureActivity;


/**
 * Created by zy on 2017/9/5.
 * 商城
 */

public class ShopActivity extends BaseMvpActivity<ShopView, ShopPresenter>
        implements ShopView, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    private View rlCategory;
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout refreshLayout;
    private LinearLayout search;
    private LinearLayoutManager layoutManager;
    private boolean isFreshing;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void initView() {
        setContentView(R.layout.fragment_shopping);
        mRecyclerView = (RecyclerView) this.findViewById(R.id.recyclerView);
        search = (LinearLayout) this.findViewById(R.id.llSearchBar);
        refreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.refreshLayout);
        rlCategory = this.findViewById(R.id.rlCategory);
        search.setOnClickListener(this);
        layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        refreshLayout.setOnRefreshListener(this);
        rlCategory.setOnClickListener(this);
    }

    @Override
    public void initData() {
        presenter.getShopData(this);
    }


    @Override
    public ShopPresenter initPresenter() {
        return new ShopPresenter();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.llSearchBar:
                Intent search = new Intent($this, SearchActivity.class);
                search.putExtra(KEY_TYPE, SEARCH_TYPE_GOODS);
                startActivity(search);
                break;
            case R.id.rlCategory:
                presenter.goPageCategory(this);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent itCapture = new Intent(this, CaptureActivity.class);
                startActivity(itCapture);

            } else {
                Toast.makeText(this, "权限被禁止，相关功能无法使用！", Toast.LENGTH_SHORT).show();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }


    @Override
    public void showProgress() {
        super.showProgress(this);
    }

    @Override
    public void onRefresh() {
        if (isFreshing) {
            return;
        } else {
            refreshLayout.setRefreshing(true);
            isFreshing = true;
            initData();
        }
    }

    @Override
    public void shopRefresh(ShopAdapter adapter) {
        refreshLayout.setRefreshing(false);
        isFreshing = false;
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
    }
}
